/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ImportDatasFrame.java
 *
 * Created on 2010-5-3, 16:28:25
 */

package fusiondata;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Administrator
 */
public class ImportDatasFrame extends javax.swing.JFrame {

    private JPanel fatherPanel;

    public ImportDatasFrame(JPanel fatherPanel) {
        this.fatherPanel = fatherPanel;
        initComponents();
    }

    /** Creates new form ImportDatasFrame */
    public ImportDatasFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dataTabbedPane = new javax.swing.JTabbedPane();
        filePanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        fileTypeCombo = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        filePathTF = new javax.swing.JTextField();
        browseFileButton = new javax.swing.JButton();
        fileNextButton = new javax.swing.JButton();
        databasePanel = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        typeDataComboBox = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        hostField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        portField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        dboField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        usernameField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        jLabel8 = new javax.swing.JLabel();
        accessField = new javax.swing.JTextField();
        testLinkButton = new javax.swing.JButton();
        DBNextButton = new javax.swing.JButton();
        browseDataFileButton = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(fusiondata.FusionDataApp.class).getContext().getResourceMap(ImportDatasFrame.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        dataTabbedPane.setName("dataTabbedPane"); // NOI18N

        filePanel.setName("filePanel"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        fileTypeCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "文本文件", "Excel文件" }));
        fileTypeCombo.setName("fileTypeCombo"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        filePathTF.setName("filePathTF"); // NOI18N

        browseFileButton.setText(resourceMap.getString("browseFileButton.text")); // NOI18N
        browseFileButton.setName("browseFileButton"); // NOI18N
        browseFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseFileButtonActionPerformed(evt);
            }
        });

        fileNextButton.setText(resourceMap.getString("fileNextButton.text")); // NOI18N
        fileNextButton.setName("fileNextButton"); // NOI18N

        javax.swing.GroupLayout filePanelLayout = new javax.swing.GroupLayout(filePanel);
        filePanel.setLayout(filePanelLayout);
        filePanelLayout.setHorizontalGroup(
            filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel9))
                .addGap(4, 4, 4)
                .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(filePathTF)
                    .addComponent(fileTypeCombo, 0, 134, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(browseFileButton)
                .addContainerGap(80, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, filePanelLayout.createSequentialGroup()
                .addContainerGap(296, Short.MAX_VALUE)
                .addComponent(fileNextButton)
                .addContainerGap())
        );
        filePanelLayout.setVerticalGroup(
            filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(fileTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(filePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(filePathTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(browseFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 158, Short.MAX_VALUE)
                .addComponent(fileNextButton)
                .addContainerGap())
        );

        dataTabbedPane.addTab(resourceMap.getString("filePanel.TabConstraints.tabTitle"), filePanel); // NOI18N

        databasePanel.setName("databasePanel"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        typeDataComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Access", "MySql", "SQL2000", "SQL2005" }));
        typeDataComboBox.setName("typeDataComboBox"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        hostField.setName("hostField"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        portField.setName("portField"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        dboField.setName("dboField"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        usernameField.setName("usernameField"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        passwordField.setName("passwordField"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        accessField.setName("accessField"); // NOI18N

        testLinkButton.setText(resourceMap.getString("testLinkButton.text")); // NOI18N
        testLinkButton.setName("testLinkButton"); // NOI18N
        testLinkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                testLinkButtonActionPerformed(evt);
            }
        });

        DBNextButton.setText(resourceMap.getString("DBNextButton.text")); // NOI18N
        DBNextButton.setName("DBNextButton"); // NOI18N
        DBNextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DBNextButtonActionPerformed(evt);
            }
        });

        browseDataFileButton.setText(resourceMap.getString("browseDataFileButton.text")); // NOI18N
        browseDataFileButton.setName("browseDataFileButton"); // NOI18N
        browseDataFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseDataFileButtonActionPerformed(evt);
            }
        });

        jButton5.setText(resourceMap.getString("jButton5.text")); // NOI18N
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout databasePanelLayout = new javax.swing.GroupLayout(databasePanel);
        databasePanel.setLayout(databasePanelLayout);
        databasePanelLayout.setHorizontalGroup(
            databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, databasePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, databasePanelLayout.createSequentialGroup()
                        .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7)
                            .addComponent(jLabel8))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(dboField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(portField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(typeDataComboBox, 0, 188, Short.MAX_VALUE)
                            .addComponent(hostField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                            .addComponent(accessField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                        .addGap(10, 10, 10)
                        .addComponent(browseDataFileButton))
                    .addGroup(databasePanelLayout.createSequentialGroup()
                        .addComponent(testLinkButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 142, Short.MAX_VALUE)
                        .addComponent(DBNextButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton5)))
                .addContainerGap())
        );
        databasePanelLayout.setVerticalGroup(
            databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(databasePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(typeDataComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hostField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dboField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(3, 3, 3)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(accessField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(browseDataFileButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(databasePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(testLinkButton)
                    .addComponent(DBNextButton)
                    .addComponent(jButton5))
                .addContainerGap())
        );

        dataTabbedPane.addTab(resourceMap.getString("databasePanel.TabConstraints.tabTitle"), databasePanel); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dataTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(dataTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

 private void testConnection() {


        DataFromDB df;
//        boolean connect = false;
        if(typeDataComboBox.getSelectedItem().equals("Access")){
            df = new UseAccess(accessField.getText().trim());
            if(df.isConnected())
            {
                JOptionPane.showMessageDialog(this, "数据库连接成功");
            }
            else
            {
                JOptionPane.showMessageDialog(this, "数据库连接失败");
            }
            df.close();

        }
        else if(typeDataComboBox.getSelectedItem().equals("MySql"))
        {
            df = new UseMySql(dboField.getText().trim(),usernameField.getText().trim(),
                    String.valueOf(passwordField.getPassword()),portField.getText().trim(),hostField.getText().trim());
            if(df.isConnected())
            {
                JOptionPane.showMessageDialog(this, "数据库连接成功");
            }
            else
            {
                JOptionPane.showMessageDialog(this, "数据库连接失败");
            }
            df.close();
        }
        else if(typeDataComboBox.getSelectedItem().equals("SQL2000"))
        {}
        else if(typeDataComboBox.getSelectedItem().equals("SQL2005"))
        {
            df = new UseSQL2005(dboField.getText().trim(),usernameField.getText().trim(),
                    String.valueOf(passwordField.getPassword()),portField.getText().trim(),hostField.getText().trim());
            if(df.isConnected())
            {
                JOptionPane.showMessageDialog(this, "数据库连接成功");
            }
            else
            {
                JOptionPane.showMessageDialog(this, "数据库连接失败");
            }
            df.close();
        }


    }


    private DataFromDB getDataFromDB(){
        DataFromDB df = null;
        if(typeDataComboBox.getSelectedItem().equals("Access"))
        {
             df = new UseAccess(accessField.getText().trim());
        }
        else if(typeDataComboBox.getSelectedItem().equals("MySql"))
        {
            df = new UseMySql(dboField.getText().trim(),usernameField.getText().trim(),
                    String.valueOf(passwordField.getPassword()),portField.getText().trim(),hostField.getText().trim());
        }
        else if(typeDataComboBox.getSelectedItem().equals("SQL2000"))
        {
        }
        else if(typeDataComboBox.getSelectedItem().equals("SQL2005"))
        {
            df = new UseSQL2005(dboField.getText().trim(),usernameField.getText().trim(),
                    String.valueOf(passwordField.getPassword()),portField.getText().trim(),hostField.getText().trim());
        }
        return df;
    }

    private void browseFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseFileButtonActionPerformed
        JFileChooser file = new JFileChooser();
        if(fileTypeCombo.getSelectedItem().equals("文本文件")) {
            file.addChoosableFileFilter(new MyFileFilter("txt", "文本文件"));
        } else {
            file.addChoosableFileFilter(new MyFileFilter("xls", "Excel文件"));
        }

        int val = file.showOpenDialog(this);
        if (val == file.APPROVE_OPTION) {
            filePathTF.setText(file.getSelectedFile().getAbsolutePath());
        }
}//GEN-LAST:event_browseFileButtonActionPerformed

    private void testLinkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_testLinkButtonActionPerformed
        testConnection();
}//GEN-LAST:event_testLinkButtonActionPerformed

    private void DBNextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DBNextButtonActionPerformed
        new DataFrame(getDataFromDB(),fatherPanel).setVisible(true);
        this.dispose();
}//GEN-LAST:event_DBNextButtonActionPerformed

    private void browseDataFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseDataFileButtonActionPerformed
        JFileChooser file = new JFileChooser();
        file.addChoosableFileFilter(new MyFileFilter("mdb", "Access文件"));
        int val = file.showOpenDialog(this);
        if (val == file.APPROVE_OPTION) {
            accessField.setText(file.getSelectedFile().getAbsolutePath());
        }
}//GEN-LAST:event_browseDataFileButtonActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton5ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ImportDatasFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton DBNextButton;
    private javax.swing.JTextField accessField;
    private javax.swing.JButton browseDataFileButton;
    private javax.swing.JButton browseFileButton;
    private javax.swing.JTabbedPane dataTabbedPane;
    private javax.swing.JPanel databasePanel;
    private javax.swing.JTextField dboField;
    private javax.swing.JButton fileNextButton;
    private javax.swing.JPanel filePanel;
    private javax.swing.JTextField filePathTF;
    private javax.swing.JComboBox fileTypeCombo;
    private javax.swing.JTextField hostField;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JTextField portField;
    private javax.swing.JButton testLinkButton;
    private javax.swing.JComboBox typeDataComboBox;
    private javax.swing.JTextField usernameField;
    // End of variables declaration//GEN-END:variables

}
